﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace StudentManage
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 新增学生
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void subAddStudent_Click(object sender, EventArgs e)
        {
            FrmAddStudent frmAddStudent = new FrmAddStudent();
            frmAddStudent.MdiParent = this; //指定父窗体，让其显示在父窗体内部
            frmAddStudent.Show(); 
        }
        /// <summary>
        /// 学生列表，不可以同时打开多个，采用单例模式或者检查是否已打开窗体（推荐）
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void subStudentList_Click(object sender, EventArgs e)
        {
            //单例模式 (不推荐，写单例较麻烦)
          /*
            FrmStudentList frmStudentList = FrmStudentList.CreateInstance();
            frmStudentList.MdiParent = this;
            frmStudentList.Show();
          */
            //先检查此窗体是否已经打开（推荐使用）
            bool bl = CheckForm(typeof(FrmStudentList).Name);
            if (!bl)
            {
                FrmStudentList frmStudentList = new FrmStudentList();
                frmStudentList.MdiParent = this;
                frmStudentList.Show();
            }            
        }
        /// <summary>
        /// 检查窗体是否已经打开
        /// </summary>
        /// <param name="formName">要检查的窗体名称</param>
        /// <returns></returns>
        private bool CheckForm(string formName)
        {
            bool flag = false;
            foreach (Form f in Application.OpenForms)
            {
                if (f.Name == formName)
                {
                    flag = true;//说明已经打开
                    f.Activate();//获取焦点
                    break;
                }
            }
            return flag;
        }
        /// <summary>
        /// 新增班级
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void subAddClass_Click(object sender, EventArgs e)
        {
            FrmAddClass frmAddClass = new FrmAddClass();
            frmAddClass.MdiParent = this;
            frmAddClass.Show();
        }
        /// <summary>
        /// 班级列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void subClassList_Click(object sender, EventArgs e)
        {
            bool bl = CheckForm(typeof(FrmClassList).Name);
            if (!bl)
            {
                FrmClassList frmClassList = new FrmClassList();
                frmClassList.MdiParent = this;
                frmClassList.Show();
            }
        }
        /// <summary>
        /// 年级列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void subGradeList_Click(object sender, EventArgs e)
        {
            bool bl = CheckForm(typeof(FrmGradeList).Name);
            if (!bl)
            {
                FrmGradeList frmGradeList = new FrmGradeList();
                frmGradeList.MdiParent = this;
                frmGradeList.Show();
            }
        }
        /// <summary>
        /// 窗体关闭，退出程序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult result = MessageBox.Show("您确定退出系统吗?","退出提示",MessageBoxButtons.YesNo,
                MessageBoxIcon.Question);
            if (result == DialogResult.Yes)
            {
                Application.ExitThread();//退出当前线程上的消息循环
            }
            else 
            {
                e.Cancel = true; //手动取消
            }
        }
        /// <summary>
        /// 退出系统按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ExitApp_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}
